<?php
namespace App\Service\Admin\Qudao;

use App\Service\Admin\BaseService as Service;

class GcateService extends Service
{
    //商品分类

    //选项
    public function getOption()
    {
        $arys = ['pid' => []];;
        $arys['pid'] = $this->db2Conn($this->dbQdGcate)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('pid', 0)
            ->get();
        return $arys;
    }

    /**
     * 列表
     */
    public function getList()
    {
        $db = $this->db2Conn($this->dbQdGcate)->where('del', 0);
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有分类');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $pOne = (array) $this->db2Conn($this->dbQdGcate)->where('id', $v['pid'])->first();
            if (file_exists($v['img'])) {
                $imgPrefix = env('DOMAIN') . '/';
            } else {
                $imgPrefix = env('FC_DOMAIN') . '/';
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'imgPrefix' => $imgPrefix,
                'imgLink' => $v['img'],
                'pid' => $v['pid'],
                'pname' => empty($pOne) ? '一级分类' : $pOne['title'],
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 列表
     */
    public function getClist()
    {
        $db = $this->db2Conn($this->dbQdGcate)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有分类');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $pOne = (array) $this->db2Conn($this->dbQdGcate)->where('id', $v['pid'])->first();
            if (file_exists($v['img'])) {
                $imgPrefix = env('DOMAIN') . '/';
            } else {
                $imgPrefix = env('FC_DOMAIN') . '/';
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'img' => $imgPrefix.$v['img'],
                'pname' => empty($pOne) ? '一级分类' : $pOne['title'],
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function addOne($pas)
    {
        $res = (array) $this->db2Conn($this->dbQdGcate)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '分类已存在');
        }
        $arys = [
            'title' => $pas['title'],
            'img' => $pas['img'],
            'pid' => $pas['pid'],
            'addtime' => toDate(),
            'uptime' => toDate(),
        ];
        $lastid = $this->db2Conn($this->dbQdGcate)->insertGetId($arys);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 添加
     * @param int $id 分类ID
     * @param array $pas
     */
    public function modify($id, $pas)
    {
        $res = (array) $this->db2Conn($this->dbQdGcate)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '分类不存在');
        }
        $res = (array) $this->db2Conn($this->dbQdGcate)
            ->where('del', 0)
            ->where('id', '<>', $id)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在同名分类');
        }
        if (!$pas['img'] && $res['img']) {
            $img = $res['img'];
        } else {
            $img = $pas['img'];
        }
        $arys = [
            'title' => $pas['title'],
            'pid' => $pas['pid'],
            'img' => $img,
            'uptime' => toDate(),
        ];
        $up = $this->db2Conn($this->dbQdGcate)->where('id', $id)->update($arys);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id 分类ID
     */
    public function setDel($id)
    {
        $res = (array) $this->db2Conn($this->dbQdGcate)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '分类不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = $this->db2Conn($this->dbQdGcate)->where('id', $id)->update($arys);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }
}